classdef ziSweeperData < EquipmentControl.ZI.ziData.ziData
    %ZISWEEPERDATA Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        default_channelName
        default_sampleName
        channel_Name
        sample_Name
    end
    
    methods
        function obj = ziSweeperData(data, varargin)
            %ZISWEEPERDATA Construct an instance of this class
            %   Detailed explanation goes here
            p = inputParser();
            p.addParameter('channel_name', 'demods', @(x) ismember(x, {'demods', 'pids'}) );
            p.parse(varargin{:});
            
            obj@EquipmentControl.ZI.ziData.ziData(data);

            obj.default_channelName = 'demods';
            obj.default_sampleName = 'sample';
            obj.channel_Name = p.Results.channel_name;
            obj.sample_Name = obj.default_sampleName;
        end
        
        function data = getNodeData(obj, channelIndex, nodeName, nodeIndex)
            if ischar(channelIndex)
                chIdx = str2double(channelIndex) + 1;
            else
                chIdx = channelIndex;
            end
            
            if ~iscell(nodeName)
                nodeNameCell = {nodeName};
            else
                nodeNameCell = nodeName;
            end
            
            if nargin == 3
                nodeIndex = 1;
            end
            
            isNodeR = strcmp(nodeNameCell, 'r');
            nodeName1 = nodeNameCell(~isNodeR);
            
            data.grid = obj.getData(obj.channel_Name, chIdx, obj.sample_Name, nodeIndex, 'grid');
            data.value = obj.getData(obj.channel_Name, chIdx, obj.sample_Name, nodeIndex, nodeName1);
            data.label = nodeName1;
            
            if any(isNodeR)
                r = obj.get_r_array(chIdx, nodeIndex);
                data.value = [data.value r];
                data.label = [data.label 'r'];
            end
        end
        
        function r = get_r_array(obj, chIdx, nodeIndex)
            xy = obj.getData(obj.channel_Name, chIdx, obj.sample_Name, nodeIndex, {'x', 'y'});
            r = sqrt(sum(xy.*xy, 2));
        end
        
        function p = plotNodeData(obj, channelIndex, nodeName, nodeIndex)
            if nargin == 3
                nodeIndex = 1;
            end
            data = obj.getNodeData(channelIndex, nodeName, nodeIndex);
            p=plot(data.grid, data.value);
        end
    end
end

